package leetcode.offer;

import java.util.HashMap;
import java.util.Set;

/**
 * ClassName: FindRepeatNumber
 * Description: leetcode-剑指offer 03 数组中重复的数组
 * date: 2021-10-31 22:23
 *
 * @author liyifan
 */
public class FindRepeatNumber {
    public int findRepeatNumber(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (Integer x:nums) {
            map.put(x, map.getOrDefault(x, 0) + 1);
        }
        Set<Integer> keys = map.keySet();
        for (Integer key:keys) {
            if (map.get(key) > 1) {
                return key;
            }
        }
        return 0;
    }

    public int findRepeatNumber2(int[] nums) {
        int[] res = new int[nums.length];
        for (int i = 0;i < nums.length;i++) {
            res[nums[i]]++;
        }
        for (int i = 0;i < res.length;i++) {
            if (res[i] > 1)
                return i;
        }
        return -1;
    }
}
